home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Zoomed Video Driver v1.0 SDK / Tools / PC Card DispNameReg / Src / DisplayNameRegistry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-02  |  23.9 KB  |  889 lines  |  [TEXT/CWIE]

  1. #include "SystemSoft.h"
  2. /*                            NameRegistryDisplayMain.c                            */
  3. /*
  4.  * NameRegistryDisplayMain.c
  5.  * Copyright © 1993-95 Apple Computer Inc. All rights reserved.
  6.  * Edit History
  7.  * 1.0a3 (94.12.23)        Release for A5 DDK
  8.  * 1.0a4 (94.12.30)        Format "reg" property.
  9.  * 1.0a5 (95.01.19)        Fix bug: "save doesn't write anything" Correctly
  10.  *                        dispose of the primary and secondary lists.
  11.  * 1.0b1 (95.03.23)        Fix bug: it is possible to have two independent registry
  12.  *                        entries with the same name. Fix the sort algorithm so
  13.  *                        they aren't smooshed together.
  14.  */
  15. #define EXTERN            /* Allocate variables */
  16.  
  17. #include "DisplayNameRegistry.h"
  18.  
  19. #include "AERegistry.h"
  20.  
  21. Boolean            gSaveAllElements = FALSE;
  22.  
  23. /*
  24.  * These enum's define items in the various menus.
  25.  */
  26. enum AppleMenu {
  27.     kAppleAbout                = 1
  28. };
  29. enum {                                            /* File Menu                    */
  30.     kFileSaveAs                = 1,
  31.     kFileUnused1,
  32.     kFilePageSetup,
  33.     kFilePrint,
  34.     kFileUnused2,
  35.     kFileQuit
  36. };
  37. enum EditMenu {
  38.     kEditUndo                = 1,
  39.     kEditUnused,
  40.     kEditCut,
  41.     kEditCopy,
  42.     kEditPaste,
  43.     kEditClear
  44. };
  45. enum OptionMenu {
  46.     kOptionSortByName        = 1,
  47.     kOptionSortByProperty,
  48.     kOptionUnused1,
  49.     kOptionRefreshDisplay,
  50.     kOptionSetFontInfo
  51. };
  52.  
  53. Boolean                    gInForeground;
  54. long                    gSleepTime;
  55.  
  56. //    Local function prototypes.
  57.  
  58. void main(void);
  59. void ProcessThisEvent( const EventRecord * eventRecordPtr, long * sleepTimePtr);
  60. void ApplicationEventLoop(void);
  61. void DoMouseEvent(const EventRecord * eventRecordPtr);
  62. void DoCommand(WindowPtr activeWindow, long menuChoice);
  63. void AdjustMenus(void);
  64. void InitMacintosh(void);
  65. void InitApplication(void);
  66. void AdjustEditMenu(Boolean isDeskAcc);
  67. static Boolean IsOurWindow(WindowPtr theWindow);
  68. void DoPageSetup(void);
  69. void DoAbout(void);
  70. pascal OSErr MyAEOpenAppHandlerFunc(long refCon);
  71. pascal OSErr MyAEQuitAppHandlerFunc(long refCon);
  72. pascal void    MyAEUnknownHandlerMsgFunc(ConstStr255Param messageText);
  73. pascal Boolean MyAEIdleProc(const EventRecord *theEventPtr, long *sleepTime, RgnHandle *mouseRgn);
  74.  
  75. RoutineDescriptor gAEIdleUPP =
  76.             BUILD_ROUTINE_DESCRIPTOR(uppAEIdleProcInfo, MyAEIdleProc);
  77.  
  78. #ifdef SYSF
  79. //--------------------------------------------------------------------------------
  80. // implement the snapshot AppleEvent
  81. pascal OSErr MyAESnapshotHandlerFunc(AppleEvent *theEvent, AppleEvent *reply, long refCon);
  82. pascal OSErr MyAESnapshotHandlerFunc(AppleEvent *theEvent, AppleEvent */*reply*/, long /*refCon*/)
  83. {
  84.     OSStatus    err;
  85.     DescType    typeCode;
  86.     char        outFileName[100];
  87.     Size        actualSize;
  88.     
  89.     // see if there is an output filename
  90.     err = AEGetParamPtr(theEvent, keyDirectObject, typeChar, &typeCode, outFileName, sizeof(outFileName), &actualSize);
  91.     if (err == noErr)
  92.         outFileName[actualSize] = 0;
  93.     else
  94.         outFileName[0] = 0;
  95.     
  96.     SnapshotToDisk(true, false, outFileName);
  97.     return (noErr);
  98. }
  99.  
  100. //--------------------------------------------------------------------------------
  101. // implement the rescan AppleEvent
  102. //pascal OSErr MyAERescanHandlerFunc(AppleEvent *theEvent, AppleEvent *reply, long refCon);
  103. //pascal OSErr MyAERescanHandlerFunc(AppleEvent *theEvent, AppleEvent */*reply*/, long /*refCon*/)
  104. //{
  105. //    OSStatus    err;
  106. //    DescType    typeCode;
  107. //    char        outFileName[100];
  108. //    Size        actualSize;
  109. //    
  110. //    // see if there is an output filename
  111. //    err = AEGetParamPtr(theEvent, keyDirectObject, typeChar, &typeCode, outFileName, sizeof(outFileName), &actualSize);
  112. //    if (err == noErr)
  113. //        outFileName[actualSize] = 0;
  114. //    else
  115. //        outFileName[0] = 0;
  116. //    
  117. //    if (gCurrentBrowserPtr != nil)
  118. //    {
  119. //        DoRefreshDisplay(gCurrentBrowserPtr);
  120. //        SnapshotToDisk(true, false, outFileName);
  121. //        return (noErr);
  122. //    }
  123. //    return (noErr);
  124. //}
  125.  
  126.  
  127. //--------------------------------------------------------------------------------
  128. // implement the rescan AppleEvent
  129. pascal OSErr MyAENoSocketsHandlerFunc(AppleEvent *theEvent, AppleEvent *reply, long refCon);
  130. pascal OSErr MyAENoSocketsHandlerFunc(AppleEvent *theEvent, AppleEvent */*reply*/, long /*refCon*/)
  131. {
  132.     OSStatus    err;
  133.     DescType    typeCode;
  134.     char        outFileName[100];
  135.     Size        actualSize;
  136.     
  137.     // see if there is an output filename
  138.     err = AEGetParamPtr(theEvent, keyDirectObject, typeChar, &typeCode, outFileName, sizeof(outFileName), &actualSize);
  139.     if (err == noErr)
  140.         outFileName[actualSize] = 0;
  141.     else
  142.         outFileName[0] = 0;
  143.     
  144.     SnapshotToDisk(true, true, outFileName);
  145.     return (noErr);
  146. }
  147.  
  148. #include <PCCard.h>
  149.  
  150. Boolean        gNeedRefresh = false;
  151.  
  152. //--------------------------------------------------------------------------
  153. //    Client callback for the sockets
  154. OSStatus CardStatusEventHandler(PCCardEvent theEvent, PCCardSocket socket, UInt32 device, UInt32 info,
  155.                                 UInt32 MTDRequest, UInt32 *buffer, UInt32 misc, UInt32 status, void *clientParam);
  156. OSStatus CardStatusEventHandler(PCCardEvent /*theEvent*/, PCCardSocket /*socket*/, UInt32 /*device*/,
  157.                                 UInt32 /*info*/, UInt32 /*MTDRequest*/, UInt32 * /*buffer*/, UInt32 /*misc*/,
  158.                                 UInt32 /*status*/, void * /*clientParam*/)
  159. {
  160. #ifdef MEMORY_HOG
  161.     // doing this was causing a bomb - using too much stack for the init
  162.     if (gCurrentBrowserPtr != nil)
  163.         DoRefreshDisplay(gCurrentBrowserPtr);
  164. #else
  165.     // this is a much more friendly callback
  166.     gNeedRefresh = true;
  167. #endif
  168.     return noErr;
  169. }
  170.  
  171. PCCardClientID            gMyClientID = -1;
  172.  
  173. //--------------------------------------------------------------------------
  174. void RegisterWithCardServices(void);
  175. void RegisterWithCardServices(void)
  176. {
  177.     RegEntryRef            wildCard;
  178.     UInt32                theClientParam = 0;
  179.     OSStatus            err;
  180.  
  181.  
  182.     err = RegistryEntryIDInit(&wildCard);
  183.     
  184.     err = PCCardRegisterClient(&wildCard, kPCCardInsertionMessage | kPCCardRemovalMessage,
  185.                      CardStatusEventHandler, (void *) theClientParam, &gMyClientID);
  186.  
  187. }
  188.  
  189. //--------------------------------------------------------------------------
  190. void DeRegisterWithCardServices(void);
  191. void DeRegisterWithCardServices(void)
  192. {
  193.     if (gMyClientID != -1)
  194.         PCCardDeRegisterClient(gMyClientID);
  195. }
  196.  
  197.  
  198. #endif
  199.  
  200. //--------------------------------------------------------------------------------
  201. //    The application main program. This is a limited program for the twist-down
  202. //    sample; it is not intended as a model for a complete Macintosh program.
  203. void main()
  204. {
  205.     OSErr                            status;
  206.     
  207.     InitMacintosh();
  208.     InitApplication();
  209.     status = InitializeAppleEvents(
  210.                 MyAEOpenAppHandlerFunc,
  211.                 NULL,                                /* No open doc handler    */
  212.                 NULL,                                /* No print doc    handler */
  213.                 MyAEQuitAppHandlerFunc,
  214.                 NULL,                                /* No DoScript callback    */
  215.                 MyAEUnknownHandlerMsgFunc,
  216.                 0                                    /* No refCon            */
  217.             );
  218.     if (status != noErr) {
  219.         if (status == gestaltUndefSelectorErr)        /* Huh? no AppleEvents?    */
  220.             status = MakeNameRegistryBrowserWindow();
  221.         if (status == noErr)
  222.             FatalError(status, "\pCan't initialize AppleEvents");
  223.     }
  224.  
  225. #ifdef SYSF
  226.     status = AEInstallEventHandler(kAEMiscStandards, kDevRegSnapshot,
  227.                 NewAEEventHandlerProc(MyAESnapshotHandlerFunc), 0, FALSE);
  228. //    status = AEInstallEventHandler(kAEMiscStandards, kDevRegRescan,
  229. //                NewAEEventHandlerProc(MyAERescanHandlerFunc), 0, FALSE);
  230.     status = AEInstallEventHandler(kAEMiscStandards, kDevRegNoSockets,
  231.                 NewAEEventHandlerProc(MyAENoSocketsHandlerFunc), 0, FALSE);
  232.     RegisterWithCardServices();
  233. #endif
  234.  
  235.     gInForeground = TRUE;
  236.     gSleepTime = 0;
  237.     InitCursor();
  238.     while (gQuitNow == FALSE) {
  239.         if (gUpdateMenusNeeded)
  240.             AdjustMenus();
  241. #ifdef SYSF
  242.         if (gNeedRefresh)    // if we got a CS notification, rescan the directory
  243.         {
  244.             if (gCurrentBrowserPtr != nil)
  245.                 DoRefreshDisplay(gCurrentBrowserPtr);
  246.             gNeedRefresh = false;
  247.         }
  248. #endif
  249.         WaitNextEvent(everyEvent, &gEventRecord, gSleepTime, NULL);
  250.         gAECoreGlobals.currentEventIsAppleEvent = FALSE;
  251.         ProcessThisEvent(&gEventRecord, &gSleepTime);
  252.     }
  253. #ifdef SYSF
  254.     DeRegisterWithCardServices();
  255. #endif
  256. }
  257.  
  258. //--------------------------------------------------------------------------------
  259. pascal OSErr MyAEOpenAppHandlerFunc(long refCon)
  260. {
  261.     OSErr                    status;
  262.     
  263.     UNUSED(refCon);
  264.     status = MakeNameRegistryBrowserWindow();
  265.     gUpdateMenusNeeded = TRUE;
  266.     return (status);
  267. }
  268.  
  269.  
  270. //--------------------------------------------------------------------------------
  271. pascal OSErr MyAEQuitAppHandlerFunc(long refCon)
  272. {
  273.     UNUSED(refCon);
  274.     gQuitNow = TRUE;
  275.     return (noErr);
  276. }
  277.  
  278.  
  279. //--------------------------------------------------------------------------------
  280. pascal void
  281. MyAEUnknownHandlerMsgFunc(ConstStr255Param messageText)
  282. {
  283.     NonFatalError(errAEHandlerNotFound, messageText);
  284. }
  285.  
  286. //--------------------------------------------------------------------------------
  287. void ProcessThisEvent(const EventRecord * eventRecordPtr, long * sleepTimePtr)
  288. {
  289.     OSErr                    status;
  290.     long                    menuChoice;
  291.     register WindowPtr        theWindow;
  292.     GrafPtr                    savePort;
  293.     Boolean                    isActivating;
  294. #define EVENT    (*eventRecordPtr)
  295.  
  296.     theWindow = FrontWindow();
  297.     switch (EVENT.what)
  298.     {
  299.         case nullEvent:
  300.             break;
  301.  
  302.         case keyDown:
  303.         case autoKey:
  304.             if ((EVENT.message & charCodeMask) == '.' && (EVENT.modifiers & cmdKey) != 0)
  305.             {
  306.                 FlushEvents(keyDown | autoKey, 0);
  307.                 gQuitNow = TRUE;
  308.             }
  309.             else if ((EVENT.modifiers & cmdKey) != 0)
  310.             {
  311.                 if (EVENT.what == keyDown)
  312.                 {
  313.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  314.                     if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
  315.                         DoCommand(theWindow, menuChoice);
  316.                     else if (IsOurWindow(theWindow))
  317.                         DoWindowKeyDown((BrowserPtr) theWindow);
  318.                     else
  319.                         SysBeep(10);
  320.                 }
  321.             }
  322.             else if (IsOurWindow(theWindow))
  323.                 DoWindowKeyDown((BrowserPtr) theWindow);
  324.             else 
  325.                 SysBeep(10);
  326.             break;
  327.  
  328.         case mouseDown:
  329.             DoMouseEvent(eventRecordPtr);
  330.             break;
  331.  
  332.         case updateEvt:
  333.             theWindow = (WindowPtr) EVENT.message;
  334.             GetPort(&savePort);
  335.             SetPort(theWindow);
  336.             BeginUpdate(theWindow);
  337.             EraseRect(&theWindow->portRect);
  338.             DrawGrowIcon(theWindow);
  339.             DrawControls(theWindow);
  340.             if (IsOurWindow(theWindow))
  341.                 UpdateBrowserWindow((BrowserPtr) theWindow, theWindow->visRgn);
  342.             EndUpdate(theWindow);
  343.             SetPort(savePort);
  344.             break;
  345.  
  346.         case activateEvt:
  347.             theWindow = (WindowPtr) EVENT.message;
  348.             isActivating = ((EVENT.modifiers & activeFlag) != 0);
  349.             goto activateEvent;
  350.             break;
  351.  
  352.         case kHighLevelEvent:
  353.             status = AEProcessAppleEvent(&EVENT);
  354.             switch (status)
  355.             {
  356.                 case userCanceledErr:
  357.                 case errAEEventNotHandled:
  358.                 case noErr:
  359.                     break;
  360.                 default:
  361.                     NonFatalError(status, "\pAppleEvent handler error");
  362.                     break;
  363.             }
  364.             break;
  365.  
  366. #ifdef SYSF
  367.         case diskEvt:
  368.             if (HiWord(EVENT.message) != noErr)
  369.             {
  370.                 Point p = {120,120};
  371.  
  372.                 DILoad();
  373.                 DIBadMount(p, EVENT.message);
  374.                 DIUnload();
  375.             }
  376. #endif
  377.  
  378.         case osEvt:
  379.             switch (((unsigned long) EVENT.message) >> 24)
  380.             {
  381.                 case mouseMovedMessage:
  382.                     break;
  383.                 case suspendResumeMessage:
  384.                     isActivating = ((EVENT.message & 0x01) != 0);
  385. activateEvent:        if (isActivating)
  386.                     {
  387.                         //    Activate this window. Activate events define theWindow from the
  388.                         //    event record, while suspend/resume uses the pre-set FrontWindow value.
  389.                         SelectWindow(theWindow);
  390.                         SetPort(theWindow);
  391.                         (void) TEFromScrap();
  392.                     }
  393.                     if (IsOurWindow(theWindow))
  394.                     {
  395.                         ActivateBrowser((BrowserPtr) theWindow, isActivating);
  396.                         //    Globalize the current window for the debugger's convenience.
  397.                         if (isActivating)
  398.                             gCurrentBrowserPtr = (BrowserPtr) theWindow;
  399.                     }
  400.                     else { /* Desk accessory or what? */ }
  401.                     gInForeground = isActivating;
  402.                     *sleepTimePtr = (gInForeground) ? 6 : 60;
  403.                     gUpdateMenusNeeded = TRUE;
  404.                     break;
  405.             }
  406.             break;
  407.     }
  408. #undef EVENT
  409. }
  410.  
  411. //--------------------------------------------------------------------------------
  412. //    The user clicked on something. Handle application-wide processing here, or call
  413. //    a Browser function for specific action.
  414. void DoMouseEvent(const EventRecord * eventRecordPtr)
  415. {
  416.     WindowPtr                theWindow;
  417.     short                    whichPart;
  418. #define EVENT    (*eventRecordPtr)
  419.         
  420.         whichPart = FindWindow(EVENT.where, &theWindow);
  421.         if (theWindow == NULL)
  422.             theWindow = FrontWindow();
  423.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  424.             theWindow = FrontWindow();
  425.         switch (whichPart)
  426.         {
  427.             case inDesk:
  428.                 break;
  429.  
  430.             case inMenuBar:
  431.                 InitCursor();
  432.                 DoCommand(theWindow, MenuSelect(EVENT.where));
  433.                 break;
  434.  
  435.             case inDrag:
  436.                 DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
  437.                 break;
  438.  
  439.             case inZoomIn:
  440.             case inZoomOut:
  441.                 if (IsOurWindow(theWindow) && TrackBox(theWindow, EVENT.where, whichPart))
  442.                 {
  443.                      DoZoomWindow(theWindow, whichPart);
  444.                      DecorateBrowserWindow((BrowserPtr) theWindow); //goto resizeWindow;
  445.                 }
  446.                 break;
  447.  
  448.             case inGrow:
  449.                 if (IsOurWindow(theWindow)
  450.                  && DoGrowWindow(theWindow, EVENT.where, kMinWindowWidth, kMinWindowHeight))
  451.                 {
  452.                     DecorateBrowserWindow((BrowserPtr) theWindow);
  453.                 }
  454.                 break;
  455.  
  456.             case inGoAway:
  457.                 if (TrackGoAway(theWindow, EVENT.where)) {
  458.                     if (IsOurWindow(theWindow))
  459.                     {
  460.                         DisposeBrowser((BrowserPtr) theWindow);
  461.                         if (gOpenWindowCount <= 0)
  462.                             gQuitNow = TRUE;
  463.                     }
  464.                     else
  465.                         SysBeep(10);
  466.                 }
  467.                 break;
  468.  
  469.             case inContent:
  470.                 if (theWindow != FrontWindow())
  471.                     SelectWindow(theWindow);
  472.                 else if (IsOurWindow(theWindow)) {
  473.                     DoContentClick((BrowserPtr) theWindow, eventRecordPtr);
  474.                 }
  475.                 else { /* Nothing happens here */ }
  476.                 break;
  477.  
  478.             default:
  479.                 break;                            /* Bogus click: ignore                */
  480.         }
  481. #undef EVENT
  482. }
  483.  
  484. //--------------------------------------------------------------------------------
  485. //    Process a menu or keystroke command.
  486. void DoCommand(WindowPtr theWindow, long menuChoice)
  487. {
  488.     short                    menuItem;
  489.     Str255                    menuText;
  490.     GrafPtr                    savePort;
  491.     BrowserPtr                browserPtr;
  492.     Boolean                    oldSortByName;
  493.     
  494.     menuItem = LoWord(menuChoice);
  495.     switch (HiWord(menuChoice))
  496.     {
  497.         case MENU_Apple:
  498.             if (menuItem == kAppleAbout)
  499.                 DoAbout();
  500.             else
  501.             {
  502.                 GetMenuItemText(gAppleMenu, menuItem, menuText);
  503.                 AdjustEditMenu(TRUE);
  504.                 GetPort(&savePort);
  505.                 OpenDeskAcc(menuText);
  506.                 SetPort(savePort);
  507.                 AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  508.             }
  509.             break;
  510.  
  511.         case MENU_File:
  512.             switch (menuItem)
  513.             {
  514.                 case kFileSaveAs:
  515.                     CreateOutputFile();
  516.                     break;
  517.                 case kFilePageSetup:
  518.                     DoPageSetup();
  519.                     break;
  520.                 case kFilePrint:
  521.                     if (IsOurWindow(theWindow))
  522.                         PrintBrowserWindow((BrowserPtr) theWindow);
  523.                     break;
  524.                 case kFileQuit:
  525.                     gQuitNow = TRUE;
  526.                     break;
  527.             }
  528.             break;
  529.  
  530.         case MENU_Edit:
  531.             if (SystemEdit(menuItem - 1) == FALSE)
  532.                 SysBeep(10);
  533.             break;
  534.  
  535.         case MENU_Options:
  536.             if (IsOurWindow(theWindow))
  537.             {
  538.                 browserPtr = (BrowserPtr) theWindow;
  539.                 oldSortByName = BROWSER.sortByName;
  540.                 switch (menuItem)
  541.                 {
  542.                     case kOptionSortByName:
  543.                         BROWSER.sortByName = TRUE;
  544.                         break;
  545.                     case kOptionSortByProperty:
  546.                         BROWSER.sortByName = FALSE;
  547.                         break;
  548.                     case kOptionRefreshDisplay:
  549.                         DoRefreshDisplay(browserPtr);
  550.                         break;
  551.                     case kOptionSetFontInfo:
  552.                         if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP)
  553.                          && SetFontInfoDialog(browserPtr))
  554.                             DoSetFontInfo(browserPtr);
  555.                         break;
  556.                 }
  557.                 if (oldSortByName != BROWSER.sortByName) {
  558.                     gUpdateMenusNeeded = TRUE;
  559.                     SortAndDisplayBrowserWindow(browserPtr);
  560.                 }
  561.             }
  562.             break;
  563.     }
  564.     HiliteMenu(0);
  565. }        
  566.  
  567. //--------------------------------------------------------------------------------
  568. //    Enable/disable menu options.
  569. void AdjustMenus(void)
  570. {
  571.     register BrowserPtr        browserPtr;
  572.     Boolean                    isOurWindow;
  573.  
  574.     isOurWindow = IsOurWindow(FrontWindow());
  575.     EnableItem(gAppleMenu, kAppleAbout);
  576.     EnableItem(gFileMenu, kFileQuit);
  577.     EnableItem(gFileMenu, kFilePageSetup);
  578.     AdjustEditMenu(isOurWindow == FALSE);
  579.     if (isOurWindow)
  580.     {
  581.         browserPtr = (BrowserPtr) FrontWindow();
  582.         EnableItem(gOptionMenu, 0);
  583.         EnableItem(gFileMenu, kFileSaveAs);
  584.         EnableItem(gFileMenu, kFilePrint);
  585.         CheckItem(gOptionMenu, kOptionSortByName, BROWSER.sortByName);
  586.         CheckItem(gOptionMenu, kOptionSortByProperty, !BROWSER.sortByName);
  587.     } else {
  588.         DisableItem(gOptionMenu, 0);
  589.         DisableItem(gFileMenu, kFileSaveAs);
  590.         DisableItem(gFileMenu, kFilePrint);
  591.     }
  592. }
  593.  
  594. //--------------------------------------------------------------------------------
  595. //    Enable/disable Edit Menu options.
  596. void
  597. AdjustEditMenu(Boolean isDeskAcc)
  598. {
  599.     if (isDeskAcc) {
  600.         EnableItem(gEditMenu, kEditUndo);
  601.         EnableItem(gEditMenu, kEditCut);
  602.         EnableItem(gEditMenu, kEditCopy);
  603.         EnableItem(gEditMenu, kEditPaste);
  604.         EnableItem(gEditMenu, kEditClear);
  605.     }
  606.     else {
  607.         DisableItem(gEditMenu, kEditUndo);
  608.         DisableItem(gEditMenu, kEditCut);
  609.         DisableItem(gEditMenu, kEditCopy);
  610.         DisableItem(gEditMenu, kEditPaste);
  611.         DisableItem(gEditMenu, kEditClear);
  612.     }
  613. }
  614.  
  615. //--------------------------------------------------------------------------------
  616. //    Perform the normal application initialization. This must be extended for "real"
  617. //    applications. The only thing this module does is initialize the managers.
  618. void InitMacintosh(void)
  619. {
  620.     int        i;
  621.     
  622.     MaxApplZone();        
  623.     InitGraf(&qd.thePort);
  624.     InitFonts();
  625.     InitWindows();
  626.     InitMenus();
  627.     TEInit();
  628.     InitDialogs(0);
  629.     HNoPurge((Handle) GetCursor(watchCursor));
  630.     SetCursor(*GetCursor(watchCursor));
  631.     for (i = 0; i < 3; i++)
  632.         EventAvail(everyEvent, &gEventRecord);
  633. }
  634.  
  635. //--------------------------------------------------------------------------------
  636. //    Continue initialization.
  637. void InitApplication(void)
  638. {        
  639.     (void) TEFromScrap();
  640.     SetMenuBar(GetNewMBar(MBAR_MenuBar));
  641.     gAppleMenu = GetMenuHandle(MENU_Apple);
  642.     gFileMenu = GetMenuHandle(MENU_File);
  643.     gEditMenu = GetMenuHandle(MENU_Edit);
  644.     gOptionMenu = GetMenuHandle(MENU_Options);
  645.     AppendResMenu(GetMenuHandle(MENU_Apple), 'DRVR');
  646.     DrawMenuBar();
  647.     SetupAnimatedCursor(ACUR_Animator);
  648. }
  649.  
  650. //--------------------------------------------------------------------------------
  651. //    Return TRUE if this is a browser window.
  652. Boolean IsOurWindow(WindowPtr theWindow)
  653. {
  654.     if (theWindow == NULL || ((WindowPeek) theWindow)->windowKind != userKind)
  655.         return (FALSE);
  656.     else
  657.         return (TRUE);
  658. }
  659.  
  660. //--------------------------------------------------------------------------------
  661. void pstrcpy(StringPtr dst, ConstStr255Param src)
  662. {
  663.     BlockMoveData(src, dst, src[0] + 1);
  664. }
  665.  
  666.  
  667. //--------------------------------------------------------------------------------
  668. void pstrcat(StringPtr dst, ConstStr255Param src)
  669. {
  670.     short                    length;
  671.     
  672.     length = 255 - dst[0];
  673.     if (length > src[0])
  674.         length = src[0];
  675.     BlockMoveData(&src[1], &dst[1] + dst[0], length);
  676.     dst[0] += length;
  677. }
  678.  
  679.  
  680. //--------------------------------------------------------------------------------
  681. //    Algorithm from New Inside Mac (Toolbox Essentials) 4-55
  682. void DoZoomWindow(WindowPtr theWindow, short whichPart)
  683. {
  684.     GDHandle                gd, gdZoom;
  685.     GrafPtr                    savePort;
  686.     Rect                    windowRect, zoomRect, intersection;
  687.     long                    thisArea, greatestArea, response;
  688.     short                    windowTitleHeight;
  689.     Boolean                    hasColorQuickDraw;
  690. #define PEEK    (*((WindowPeek) theWindow))
  691. #define STATE    (**((WStateDataHandle) PEEK.dataHandle))
  692.  
  693.     GetPort(&savePort);
  694.     SetPort(theWindow);
  695.  
  696.     hasColorQuickDraw = FALSE;
  697.     if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
  698.      && response >= gestalt8BitQD)
  699.          hasColorQuickDraw = TRUE;
  700.     if (whichPart == inZoomOut) {
  701.         if (hasColorQuickDraw == FALSE) {
  702.  
  703.             //    This shouldn't happen on a modern system, but, if it does, just
  704.             //    force a single screen zoom.
  705.             zoomRect = qd.screenBits.bounds;
  706.             InsetRect(&zoomRect, 4, 4);
  707.             STATE.stdState = zoomRect;
  708.         }
  709.         else {
  710.             //    We have color QuickDraw. Locate the screen that contains
  711.             //    the largest area of the window and zoom to that screen.
  712.             windowRect = theWindow->portRect;
  713.             LocalToGlobal(&topLeft(windowRect));
  714.             LocalToGlobal(&botRight(windowRect));
  715.             windowTitleHeight = windowRect.top - 1 - (**PEEK.strucRgn).rgnBBox.top;
  716.             windowRect.top -= windowTitleHeight;
  717.             greatestArea = 0;
  718.             gdZoom = NULL;
  719.  
  720.             //    Look at all graphics devices and find an intersection. Then
  721.             //    select the largest intersecting screen.
  722.             for (gd = GetDeviceList(); gd != NULL; gd = GetNextDevice(gd)) {
  723.                 if (TestDeviceAttribute(gd, screenDevice)
  724.                  && TestDeviceAttribute(gd, screenActive)
  725.                  && SectRect(&windowRect, &(**gd).gdRect, &intersection)) {
  726.                     thisArea = ((long) width(intersection)) * ((long) height(intersection));
  727.                     if (thisArea > greatestArea) {
  728.                         greatestArea = thisArea;
  729.                         gdZoom = gd;
  730.                     }
  731.                 }
  732.             }
  733.             //    If we're zooming to the device with the menu bar,
  734.             //    allow for its height.
  735.             if (GetMainDevice() == gdZoom)
  736.                 windowTitleHeight += GetMBarHeight();
  737.             zoomRect = (**gdZoom).gdRect;
  738.             InsetRect(&zoomRect, 3, 3);
  739.             zoomRect.top += windowTitleHeight;
  740.             STATE.stdState = zoomRect;
  741.         }                                        /* End if color QuickDraw    */
  742.     }                                            /* End if zoom out            */
  743.     ZoomWindow(theWindow, whichPart, (theWindow == FrontWindow()));
  744.  
  745.     //    Zoom redraws the entire window.
  746.     EraseRect(&theWindow->portRect);
  747.     InvalRect(&theWindow->portRect);
  748.     SetPort(savePort);            
  749. #undef PEEK
  750. #undef STATE
  751. }
  752.  
  753. //--------------------------------------------------------------------------------
  754. //    Algorithm from New Inside Mac (Toolbox Essentials) 4-58 (simplified)
  755. //    We assume that the window can cover the entire screen.
  756. Boolean DoGrowWindow(WindowPtr theWindow, Point startingPoint, short minimumWidth, short minimumHeight)
  757. {
  758.     long                    growSize;
  759.     Rect                    limitRect;
  760.     
  761.     limitRect.left = minimumWidth;
  762.     limitRect.top = minimumHeight;
  763.     limitRect.right = qd.screenBits.bounds.right;
  764.     limitRect.bottom = qd.screenBits.bounds.bottom;
  765.     growSize = GrowWindow(theWindow, startingPoint, &limitRect);
  766.     if (growSize != 0) {
  767.         SizeWindow(theWindow, LoWord(growSize), HiWord(growSize), TRUE);
  768.  
  769.         //    Force a redraw of the entire window, (The correct algorithm
  770.         //    excludes the intersection of the old view rect and the new view
  771.         //    rect, but this doesn't work correctly yet.) Invalidate any prior
  772.         //    update region in any case.
  773.         EraseRect(&theWindow->portRect);
  774.         InvalRect(&theWindow->portRect);
  775.     }
  776.     return (growSize != 0);
  777. }
  778.  
  779. //--------------------------------------------------------------------------------
  780. void
  781. DoPageSetup(void)
  782. {
  783.     OSErr                    status;
  784.     
  785.     PrOpen();
  786.     status = PrError();
  787.     if (status != noErr)
  788.         NonFatalError(status, "\pPrinting disabled");
  789.     else {
  790.         if (gPrintHandle == NULL) {
  791.             gPrintHandle = (THPrint) NewHandle(sizeof (TPrint));
  792.             if (gPrintHandle != NULL)
  793.                 PrintDefault(gPrintHandle);
  794.         }
  795.         if (gPrintHandle != NULL)
  796.             (void) PrStlDialog(gPrintHandle);
  797.         PrClose();
  798.     }
  799. }
  800.  
  801. //--------------------------------------------------------------------------------
  802. void DoAbout(void)
  803. {
  804.     GrafPtr                    savePort;
  805.     DialogPtr                dialog;
  806.     short                    dialogItem;
  807.  
  808.     dialog = GetNewDialog(DLOG_About, NULL, (WindowPtr) -1L);
  809.     if (dialog != NULL) {
  810.         GetPort(&savePort);
  811.         SetPort(dialog);
  812.         ShowWindow(dialog);
  813.         ModalDialog(NULL, &dialogItem);
  814.         DisposeDialog(dialog);
  815.         SetPort(savePort);
  816.     }
  817. }
  818.  
  819. //--------------------------------------------------------------------------------
  820. void
  821. CheckError( OSErr errorStatus, ConstStr255Param errorMsg)
  822. {
  823.     if (errorStatus != noErr && errorStatus != userCanceledErr)
  824.         NonFatalError(errorStatus, errorMsg);
  825. }
  826.  
  827. //--------------------------------------------------------------------------------
  828. void NonFatalError( OSErr errorStatus, ConstStr255Param errorMsg)
  829. {
  830.     if (ErrorMessage(ALRT_NonFatalError, errorStatus, errorMsg) == kOKButton)
  831.         gQuitNow = TRUE;
  832. }
  833.  
  834. //--------------------------------------------------------------------------------
  835. void FatalError( OSErr errorStatus, ConstStr255Param errorMsg)
  836. {
  837.     ErrorMessage(ALRT_FatalError, errorStatus, errorMsg);
  838.     ExitToShell();
  839. }
  840.  
  841.  
  842. //--------------------------------------------------------------------------------
  843. short ErrorMessage(
  844.         short                    alertID,
  845.         OSErr                    errorStatus,
  846.         ConstStr255Param        errorMsg)
  847. {
  848.     Handle                    errorTextHdl;
  849.     StringPtr                errorTextPtr;
  850.     Str15                    errorStatusText;
  851.     short                    result;
  852.     
  853.     if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP) == FALSE)
  854.         result = kCancelButton;
  855.     else {
  856.         NumToString(errorStatus, errorStatusText);
  857.         errorTextHdl = GetResource('Estr', errorStatus);
  858.         if (errorTextHdl != NULL) {
  859.             HLock(errorTextHdl);
  860.             errorTextPtr = (StringPtr) errorTextHdl;
  861.         }
  862.         else {
  863.             errorTextPtr = "\pSystem Error";
  864.         }
  865.         ParamText(errorStatusText, errorTextPtr, errorMsg, "\p");
  866.         InitCursor();
  867.         result = StopAlert(alertID, NULL);
  868.         if (errorTextHdl != NULL)
  869.             ReleaseResource(errorTextHdl);
  870.     }
  871.     return (result);
  872. }
  873.  
  874.  
  875. //--------------------------------------------------------------------------------
  876. pascal Boolean MyAEIdleProc(
  877.         const EventRecord        *theEventPtr,
  878.         long                    *sleepTime,
  879.         RgnHandle                *mouseRgn)
  880. {
  881.     UNUSED(mouseRgn);
  882.     if (theEventPtr->what == kHighLevelEvent)
  883.         return (TRUE);
  884.     else {
  885.         ProcessThisEvent(theEventPtr, sleepTime);    /* Application specific    */
  886.         return (FALSE);
  887.     }
  888. }        
  889.